import { _decorator, Button, Component, Label, Node, Sprite } from 'cc';
import { Tech } from '../entity/Tech';
import { Soldier } from '../entity/Soldier';
import { Events } from '../entity/Events';
import { Utils } from '../common/Utils';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { UserInfo } from '../common/UserInfo';
import { EventOper } from '../entity/Buildings';
import VillageApi from '../api/VillageApi';
const { ccclass, property } = _decorator;

@ccclass('Upgrading')
export class Upgrading extends Component {
    @property(Node)
    bg: Node
    @property(Node)
    close: Node
    @property(Node)
    upgrade: Node
    @property(Label)
    title: Label
    @property(Sprite)
    cover: Sprite
    @property(Sprite)
    border: Sprite
    @property(Label)
    soldierName: Label
    @property(Label)
    curAttack: Label
    @property(Label)
    curLevel: Label
    @property(Label)
    nextLevel: Label
    @property(Label)
    nextAttack: Label

    @property(Label)
    time: Label

    techData:Tech
    offTime:number

    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.bg.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.upgrade.on(Node.EventType.TOUCH_END, this.onSpeed, this)
    }   

    async setData(tech:Tech) {
        this.techData = tech
        this.title.string = tech.name + '强化'
        this.soldierName.string = tech.name
        let soldier = Soldier.createById(this.techData.id)
        soldier.attackLevel = this.techData.level
        let ad = soldier.getAttackAdvanced()
        console.log(ad)
        this.curAttack.string = (ad.addition * 100).toFixed(1) + '%'
        this.curLevel.string = '当前等级' + ad.level
        this.nextAttack.string = (ad.additionNext * 100).toFixed(1) + '%'
        this.nextLevel.string = '下一等级' + (ad.level + 1)

        this.cover.spriteFrame = await tech.getTechImg()

        let event = tech.getStudyEvent()
        if (event) {
            this.countDown(event)
        }

        // this.time.string = Utils.secondToStr(ad.time) 
    }

    countDown(event: Events) {
        this.unschedule(this.updateTime)
        if (!event) {
            return
        }

        let eStartTime = event.eStartTime
        this.offTime = eStartTime - Math.floor(UserInfo.instance.serverTime / 1000)
        if (this.offTime <= 0) {
            this.onClose()
            return
        }

        this.time.string = `剩余时间：${Utils.secondToStr(this.offTime)}`
        this.schedule(this.updateTime, 1, this.offTime - 1)
    }

    async onSpeed() {
        let event = this.techData.getStudyEvent()
        if(event) {
            let param = {
                ac: EventOper.SPEED_BUILD,
                eid: event.eHandle
            }
    
            await UIManager.instance.showLoading()
            let res = await VillageApi.eventOper(param)
            UIManager.instance.closeLoading()
            console.log(`加速升级，ac =${param.ac} ,del =${param.eid} `, res)
    
            await UserInfo.instance.refreshVillageDetail()
            event = this.techData.getStudyEvent()
            if(event) {
                let eStartTime = event.eStartTime
                this.offTime = eStartTime - Math.floor(UserInfo.instance.serverTime / 1000)
                if (this.offTime <= 0) {
                    this.onClose()
                    return;
                }
        
                this.time.string = `${Utils.secondToStr(this.offTime)}`
                this.schedule(this.updateTime, 1, this.offTime - 1)
            } else {
                this.onClose()
            }
        }

        this.onClose()
    }

    updateTime() {
        this.offTime--
        this.time.string = `剩余时间：${Utils.secondToStr(this.offTime)}`

        if (this.offTime <= 0) {
            // 倒计时完成
            // 清除事件，刷新页面
           
            UserInfo.instance.refreshVillageDetail()
            this.onClose()
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.Upgrading)
    }
}


